package com.ctp.thread.daemon;

import javax.swing.table.DefaultTableModel;

import com.ctp.bean.FormBean;
import com.ctp.bean.MarketBean;
import com.ctp.controller.manager.TraderManager;

public class ProfitUpdateThread extends Thread implements Runnable {

	private DefaultTableModel singleModel = null;

	private TraderManager traderManager = TraderManager.instance();

	public void setSingleTableModel(DefaultTableModel singleModel) {
		this.singleModel = singleModel;
	}

	public void run() {
		MarketBean[] markets = traderManager.getMarkets();

		while (true) {
			for (int i = 0; i < singleModel.getRowCount(); i++) {
				String formNum = (String) singleModel.getValueAt(i, 0);
				FormBean formBean = traderManager.getFormByNum(formNum);

				if (formBean != null) {
					double diffMove = markets[2].getLastPrice()
							- formBean.getOriginDiffPrice();
					double leg1Move = markets[0].getLastPrice()
							- formBean.getOriginLeg1Price();
					double leg2Move = markets[1].getLastPrice()
							- formBean.getOriginLeg2Price();

					singleModel.setValueAt(diffMove, i, 6);
					singleModel.setValueAt(leg1Move, i, 7);
					singleModel.setValueAt(leg2Move, i, 8);

					double prftLoss = (markets[0].getLastPrice() - formBean
							.getBargainPrice()) * formBean.getFormVolume();

					prftLoss *= formBean.isBuySellInd() ? 1 : -1;
					prftLoss *= TraderManager.PER_UNIT_TIMES;

					singleModel.setValueAt(prftLoss, i, 9);
				}
			}

			try {
				Thread.sleep(500);
			} catch (Exception e) {
			}
		}
	}
}
